﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using RMS.Model;
using RMS.View;

namespace RMS.Controller
{
    public class AcademicDutyController
    {
        private Teacher _model;
        private AcademicDutyView _view;

        public AcademicDutyController(Teacher model, AcademicDutyView view)
        {
            this._model = model;
            this._view = view;
        }
        public Teacher Model
        {
            get { return this._model; }
            set { this._model = value; }
        }
        public AcademicDutyView View
        {
            get { return this._view; }
            set { this._view = value; }
        }

        public void SyncTeacher()
        {
            this.Model = Persistence.DataManager.Instance.SyncTeacherWithDatabase(this.Model.Id);
            this.PopulateAcademicProgramList();
        }

        public void PopulateAcademicProgramList()
        {
            List<AcademicProgram> listPrograms = Persistence.DataManager.Instance.SyncTeacherAcademicPrograms(this._model.Id);
            this._view.RefreshProgramList(listPrograms);           
        }

        public void PopulateDisciplineList(int programId)
        {
            List<Discipline> listDiscipline = new List<Discipline>();
            foreach (AcademicDuty duty in this._model.Duties)
            {
                if (duty.ProgramId == programId)
                {
                    foreach (Tuple<Discipline, int, int, int> tuple in duty.DisciplineScheduling)
                    {
                        listDiscipline.Add(tuple.Item1);
                    }
                }
            }
            this._view.RefreshDisciplineList(listDiscipline);
        }

        public void PopulateScheduleList(int programId, int disciplineId)
        {
            List<Tuple<Discipline, int, int, int>> scheduleList = new List<Tuple<Discipline, int, int, int>>();
            foreach (AcademicDuty duty in this._model.Duties)
            {
                if(duty.ProgramId == programId)
                {
                    foreach (Tuple<Discipline, int, int, int> tuple in duty.DisciplineScheduling)
                    {
                        if (tuple.Item1.Id == disciplineId)
                            scheduleList.Add(tuple);
                    }
                }
            }
            this._view.RefreshScheduleList(scheduleList);
        }
    }
}
